package com.example.administrator.dc.Activity;

import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.administrator.dc.R;
import com.example.administrator.dc.fragment.PeixunFragment;
import com.example.administrator.dc.fragment.HomeFragment;
import com.example.administrator.dc.fragment.ThreeFragment;
import com.example.administrator.dc.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

import static android.R.color.black;
import static com.example.administrator.dc.R.color.green;

public class MainActivity extends BaseActivity {
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private HomeFragment homeFragment;
    private TwoFragment twoFragment;
    private ViewPager vp;

    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initEvents();
        initFragment();


    }
    private  void init(){
        radioButton1= (RadioButton) findViewById(R.id.radioButton);
        radioButton2= (RadioButton) findViewById(R.id.radioButton2);
      //  radioButton3= (RadioButton) findViewById(R.id.radioButton3);
        vp= (ViewPager) findViewById(R.id.viewPager);


        fragmentManager=getSupportFragmentManager();      //getChildFragmentManager();在fragment

        fragmentTransaction=fragmentManager.beginTransaction();
        homeFragment=new HomeFragment();
        twoFragment=new TwoFragment();
       // threeFragment=new ThreeFragment();
        //  fourFragment=new FourFragment();

        fragmentTransaction.add(R.id.viewPager,homeFragment);
        fragmentTransaction.add(R.id.viewPager,twoFragment);
        //fragmentTransaction.add(R.id.viewPager,threeFragment);
        //fragmentTransaction.add(R.id.framelayout,fourFragment);

        fragmentTransaction.commit();

    }

    private  void initEvents(){

        radioButton1.setOnClickListener(onClickListener);
        radioButton2.setOnClickListener(onClickListener);
    //    radioButton3.setOnClickListener(onClickListener);


    }
    View.OnClickListener  onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.radioButton:
                    Toast.makeText(MainActivity.this, "用户名登录", Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.radioButton2:
                    Toast.makeText(MainActivity.this, "手机号登录", Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
            }



        }
    };
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                radioButton1.setTextColor(getResources().getColor(green));
                radioButton2.setTextColor(getResources().getColor(black));
                break;
            case 1:
                radioButton1.setTextColor(getResources().getColor(black));
                radioButton2.setTextColor(getResources().getColor(green));

                break;
        }
        vp.setCurrentItem(i);
    }
    private void initFragment(){
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new HomeFragment());
        listfragment.add(new TwoFragment());
    //    listfragment.add(new ThreeFragment());

        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp.setAdapter(fpadapter);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                int i = Integer.parseInt(String.valueOf(position));
                vp.setCurrentItem(position);
                if(i==0){


                    fragmentManager=getSupportFragmentManager();
                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.show(homeFragment);
                    fragmentTransaction.hide(twoFragment);
                //    fragmentTransaction.hide(threeFragment);
                    fragmentTransaction.commit();


                }
                if(i==1){
                                fragmentTransaction = fragmentManager.beginTransaction();
                                fragmentTransaction.show(twoFragment);
                                fragmentTransaction.hide(homeFragment);
                             //   fragmentTransaction.hide(threeFragment);
                                fragmentTransaction.commit();

                }
            /*    if(i==2){

                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.show(threeFragment);
                    fragmentTransaction.hide(twoFragment);
                    fragmentTransaction.hide(homeFragment);
                    fragmentTransaction.commit();

                }*/

             /*   resetImage();
                resetText();
                selectTab(position);*/
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

}
